package org.smart4j.chapter1.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.smart4j.chapter1.model.Customer;
import org.smart4j.chapter1.service.CustomerService;

/**
 * Servlet implementation class CustomerServlet
 */
@WebServlet(name = "customer", urlPatterns = { "/customer" })
public class CustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private CustomerService customerService;
	
    @Override
	public void init() throws ServletException {
    	System.out.println(11);
		customerService = new CustomerService();
	}

	/**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 进入客户列表界面
	 * 
	 * 转发和重定向的区别
	 * http://www.cnblogs.com/shenliang123/archive/2011/10/27/2226892.html
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Customer> customerList = customerService.getCustomerList();
		request.setAttribute("customerList", customerList);
		request.getRequestDispatcher("/WEB-INF/view/customer.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
